#include "CRendererOGL.h"
#include "CRenderTargetOGL.h"

/// OpenGL Renderer Contructor
CRendererOGL::CRendererOGL( ) : m_Surf_Display(0), m_pBackbuffer(0) {
}

/// OpenGL Renderer Destructor
CRendererOGL::~CRendererOGL( ) {
	// We free the dirty region
	SDL_FreeSurface(m_Surf_Display);
	// Delete the back buffer representation
	FREE( m_pBackbuffer );
	// And close the display window
	SDL_Quit();
}

/// Initialize opengl renderer
/**	@param w - screen/window width
		@param h - screen/window height
		@param b - screen/window bitrate
		@param fs - if true, renderer will run in fullscreen
		@return 1 of successful */
int CRendererOGL::Initialize( int w, int h, int b, bool fs ) {
	if( m_pBackbuffer )
		return 1;
	#ifdef TZM_BUILD_SAFEMODE
		if( SDL_Init(SDL_INIT_VIDEO) < 0 ) return 0;
	#else
		SDL_Init(SDL_INIT_VIDEO)
	#endif

	SDL_GL_SetAttribute( SDL_GL_DOUBLEBUFFER, 1 );

	if( fs ) m_Surf_Display = SDL_SetVideoMode( w, h, b, SDL_OPENGL | SDL_FULLSCREEN );
	else 		m_Surf_Display = SDL_SetVideoMode( w, h, b, SDL_OPENGL );

	#ifdef TZM_BUILD_SAFEMODE
		if( !m_Surf_Display ) return 0;
	#endif

	glewInit();

	m_pBackbuffer = new CRenderTargetOGL( w, h );

	// Return a success
	return 1;
}

/// Swap the buffers
void CRendererOGL::Present( ) {
	SDL_GL_SwapBuffers( );
}
